<?php
/**
 * KayPaste
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category   KayPaste
 * @package    User
 * @subpackage Forms
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */

/**
 * @see Zend_Form
 */
require_once 'Zend/Form.php';
require_once 'Zend/Form/Element/Text.php';
require_once 'Zend/Form/Element/Password.php';
require_once 'Zend/Form/Element/Submit.php';

/**
 * @see Zym_Validate_Confirm
 */
require_once 'Zym/Validate/Confirm.php';

/**
 * User_Form_Register
 * 
 * Form for registering as a user.
 *
 * @category   KayPaste
 * @package    User
 * @subpackage Forms
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */
class User_Form_Register extends Zend_Form
{
    /**
     * Initializes form
     * 
     */
    public function init()
    {
        // call parent for good measure
        parent::init();
        
        $this->setMethod('post');
        $this->setAttrib('id', 'register-form');
        
        //
        // create elements
        //
        $realname = new Zend_Form_Element_Text('realname');
        $email    = new Zend_Form_Element_Text('email');
        $username = new Zend_Form_Element_Text('username');
        $pass1    = new Zend_Form_Element_Password('password1');
        $pass2    = new Zend_Form_Element_Password('password2');
        $submit   = new Zend_Form_Element_Submit('submit');
        
        $this->addElement($realname);
        $this->addElement($email);
        $this->addElement($username);
        $this->addElement($pass1);
        $this->addElement($pass2);
        $this->addElement($submit);
        
        //
        // setup 'realname' element
        //
        $realname->setRequired(true)
                 ->setLabel('Your name')
                 ->setAttrib('id', 'register-realname')
                 ->setAttrib('size', 30)
                 ->setAttrib('maxlength', 64)
                 ->setAttrib('class', 'text')
                 ->addFilters(array('StringTrim'))
                 ->addValidators(array(
                     array('NotEmpty', true),
                     array('StringLength', true, array(4, 64))
                 ));
        
        //
        // setup 'email' element
        //
        $email->setRequired(true)
              ->setLabel('Email address')
              ->setAttrib('id', 'register-email')
              ->setAttrib('size', 30)
              ->setAttrib('maxlength', 96)
              ->setAttrib('class', 'text')
              ->addFilters(array('StringTrim'))
              ->addValidators(array(
                  array('NotEmpty', true),
                  array('EmailAddress', true)
                  // TODO: add validator to check if email exists
              ));
        
        //
        // setup 'username' element
        //
        $username->setRequired(true)
                 ->setLabel('Desired username')
                 ->setAttrib('id', 'register-username')
                 ->setAttrib('size', 30)
                 ->setAttrib('maxlength', 16)
                 ->setAttrib('class', 'text')
                 ->addFilters(array('StringTrim'))
                 ->addValidators(array(
                     array('NotEmpty', true),
                     array('StringLength', true, array(0, 64)),
                 ));
        
        //
        // setup 'password1' element
        //
        $pass1->setRequired(true)
              ->setLabel('Password')
              ->setAttrib('id', 'register-password1')
              ->setAttrib('size', 30)
              ->setAttrib('maxlength', 128)
              ->setAttrib('class', 'text')
              ->addFilters(array('StringTrim'))
              ->addValidators(array(
                  array('NotEmpty', true),
                  array('StringLength', true, array(4, 128)),
                  // TODO: add validator to check if username exists
              ));
        
        //
        // setup 'password2' element
        //
        $pass2->setRequired(true)
              ->setLabel('Repeat password')
              ->setAttrib('id', 'register-password2')
              ->setAttrib('size', 30)
              ->setAttrib('maxlength', 128)
              ->setAttrib('class', 'text')
              ->addFilters(array('StringTrim'))
              ->addValidators(array(
                  // password2 must be the same as password1
                  new Zym_Validate_Confirm('password1')
              ));
        
        //
        // setup submit button
        //
        $submit->setLabel('Count me in!')
               ->setAttrib('id', 'register-submit')
               ->setAttrib('class', 'button');
    }
}
